<?php
/**
 * 异步查询
**/
namespace app\sysadmin\controller;
class Ajaxuploads extends Common{
	public function __construct(){
		parent::__construct();
	}
	public function ajax_upload(){
		if($this->request->isAjax() != true){
			return $this->tips_404();
			exit;
		}
		$_post_data = input('post.');
		$_file_data = $this->request->file('file');
		
		$_path = 'upload_file';
		
		$_savepath = $_post_data['savepath'];
		if($_savepath){
			$_savepath = str_replace('\\','/',$_savepath);
			$_son_path = explode('/',strtolower($_savepath));
			foreach($_son_path as $k=>$v){
				if(!preg_match('/^[A-Za-z0-9_]{1,30}$/',$v)){
					$_json['code'] = 0;
					$_json['msg'] = '上传路径错误';
					echo json_encode($_json);exit;
				}else{
					$_path .= '/'.$v;
				}
			}
		}
		$info = $_file_data->rule('date')->move($_path);
		$_json = array();
		$_json['data'] = array();
		// $_json['data']['_post'] = $_post_data;
		// $_json['data']['_files'] = $_FILES;
		if($info){
			// 成功上传后 获取上传信息
			$_json['code'] = 1;
			$_json['msg'] = '上传成功';
			// 输出 jpg
			$_json['data']['ext'] = $info->getExtension();
			// 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
			$_json['data']['url'] = str_replace('\\','/','/'.$_path.'/'.$info->getSaveName());
			// 输出 42a79759f284b767dfcb2a0197904287.jpg
			$_json['data']['filename'] = $info->getFilename(); 
		}else{
			$_json['code'] = 0;
			$_json['msg'] = $_file_data->getError();
		}
		echo json_encode($_json);exit;
	}
	public function layer_upload_static(){
		
		$_get_data = isset($_GET)?$_GET:array();
		$path = '';
		$_key_url = '';
		$_auth_suffix = array();
		if($_get_data){
			foreach($_get_data as $k=>$v){
				if($k != 'path'){
					$_key_url .= $k.'='.$v.'&';
				}else{
					$path = $_get_data['path'];
					unset($_get_data['path']);
				}
				if($k == 'exts' && $v){
					$_auth_suffix = explode('|',strtolower($v));
				}
			}
		}
		$path_lists = array();
		$_path_str = '';
		if($path){
			$_p_lists = explode('/',$path);
			foreach($_p_lists as $v){
				if(preg_match('/^[_0-9a-z]{1,16}$/',$v)){
					$_path_str = $_path_str ? $_path_str.'/'.$v : $v;
					
					$path_lists[] = array(
						'name' => $v,
						'son_path' => $_path_str,
					);
				}
			}
		}
		$_path = '/upload_file';
		$_f_path = '.'.$_path.($_path_str?'/'.$_path_str:'');
		if(!is_dir($_f_path)){
			mkdir ($_f_path,0777,true);
		}
		$glob = new \FilesystemIterator($_f_path,  \FilesystemIterator::KEY_AS_FILENAME);
		
		$_lists_dir = array();
		$_lists_upload = array();
		foreach ($glob as $name => $file){
			// echo $file->getFileName();//文件名称
			
			// echo iconv("utf-8","gb2312",$name);
			$name = iconv('GB2312', 'UTF-8',$name);
			// echo $name;
			$_is_dir = $file->isDir() ? 1 : 0;
			if($_is_dir == 1){
				$_url = '/static/sysadmin/exts/dir.png';
				$_lists_dir[] = array(
					'name' => $name,
					'is_dir' => $_is_dir,
					'url' => $_url,
					'son_path' => ($_path_str?$_path_str.'/':'').$name,
				);
				
				
			}else{
				
				$_url = $_path.($_path_str?'/'.$_path_str:'').'/'.$name;
				$_name_arr = explode('.',$name);
				$_suffix = end($_name_arr);
				
				if(!$_auth_suffix || in_array($_suffix,$_auth_suffix)){
					
					if(in_array($_suffix,array('jpg','png','gif','bmp','jpeg'))){
						$_url = $_path.($_path_str?'/'.$_path_str:'').'/'.$name;
					}else{
						$_url = '/static/sysadmin/exts/'.$_suffix.'.png';
					}
					
					$_lists_upload[] = array(
						'name' => $name,
						'is_dir' => $_is_dir,
						'url' => $_url,
						'suffix' => $_suffix,
					);
				}
				
			}
		}
		// print_r($_lists_dir);
		// print_r($_lists_upload);
		
		$this->assign('get_data',$_get_data);
		$this->assign('key_url',$_key_url);
		$this->assign('path_lists',$path_lists);
		$this->assign('lists_dir',$_lists_dir);
		$this->assign('lists_upload',$_lists_upload);
		return $this->fetch();
	}
}
?>